package com.ffcc.fitness.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.ffcc.fitness.base.response.JsonResult;
import com.ffcc.fitness.order.pojo.dto.OrderCreateParam;
import com.ffcc.fitness.order.pojo.dto.OrderStatusUpdateParam;
import com.ffcc.fitness.order.pojo.vo.OrderVO;
import com.ffcc.fitness.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/order/")
@SaCheckLogin
public class OrderController {
    
    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping("create")
    public JsonResult createOrder(@RequestBody OrderCreateParam orderCreateParam) {
        try {
            log.info("接收创建订单请求：{}", orderCreateParam);
            
            // 设置当前登录用户为买家
            orderCreateParam.setBuyerId(StpUtil.getLoginIdAsLong());
            
            OrderVO orderVO = orderService.createOrder(orderCreateParam);
            
            return JsonResult.ok(orderVO);
        } catch (RuntimeException e) {
            log.error("创建订单失败：{}", e.getMessage());
            return new JsonResult(e.getMessage());
        }
    }
    
    /**
     * 检查用户地址
     */
    @GetMapping("check-address")
    public JsonResult checkUserAddress() {
        try {
            Long userId = StpUtil.getLoginIdAsLong();
            boolean hasAddress = orderService.checkUserAddress(userId);
            
            return JsonResult.ok(hasAddress);
        } catch (Exception e) {
            log.error("检查用户地址异常", e);
            return new JsonResult(e.getMessage());
        }
    }
    
    /**
     * 获取我的购买订单（买家视角）
     */
    @GetMapping("my-purchases")
    public JsonResult getMyPurchases() {
        try {
            Long buyerId = StpUtil.getLoginIdAsLong();
            List<OrderVO> orders = orderService.getOrdersByBuyerId(buyerId);
            
            return JsonResult.ok(orders);
        } catch (Exception e) {
            log.error("查询购买订单失败", e);
            return new JsonResult(e.getMessage());
        }
    }
    
    /**
     * 获取我的销售订单（卖家视角）
     */
    @GetMapping("my-sales")
    public JsonResult getMySales() {
        try {
            Long sellerId = StpUtil.getLoginIdAsLong();
            List<OrderVO> orders = orderService.getOrdersBySellerId(sellerId);
            
            return JsonResult.ok(orders);
        } catch (Exception e) {
            log.error("查询销售订单失败", e);
            return new JsonResult(e.getMessage());
        }
    }
    
    /**
     * 更新订单状态
     */
    @PostMapping("update-status")
    public JsonResult updateOrderStatus(@RequestBody OrderStatusUpdateParam updateParam) {
        try {
            log.info("接收订单状态更新请求：{}", updateParam);
            
            orderService.updateOrderStatus(updateParam);
            
            return JsonResult.ok("订单状态更新成功");
        } catch (RuntimeException e) {
            log.error("更新订单状态失败：{}", e.getMessage());
            return new JsonResult(e.getMessage());
        }
    }
}
